/**
 * 数组工具类
 */
const SPLIT_REG_EXP = /[,，]/

// 字符串 转 数组
export function strToArr(str) {
  let list = []
  if (str) {
    list = str.split(SPLIT_REG_EXP)
  }
  return list
}

// 数组 转 字符串
export function arrToStr(list) {
  let str = ''
  if (list) {
    str = list.join(',')
  }
  return str
}

// 遍历树形数组
export function getTreeNodeById(arr, id, idField = 'id') {
  let res = null
  arr.forEach(item => {
    if (res) {
      return res
    }
    if (item[idField] === id) {
      res = item
    } else if (item.children) {
      res = getTreeNodeById(item.children, id, idField)
    }
  })
  return res
}

// 扁平化数组
export function flatten(arr) {
  var result = []
  for (var i = 0, len = arr.length; i < len; i++) {
    if (Array.isArray(arr[i])) {
      result = result.concat(flatten(arr[i]))
    } else {
      result.push(arr[i])
    }
  }
  return result
}

export function removeArrItem(arr, item) {
  if (arr && item) {
    const index = arr.indexOf(item)
    if (index !== -1) {
      this.fileList.splice(index)
    }
  }
}
